package com.zhentao;

import com.zhentao.properties.MinioProperties;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhentao.domain.Area;
import com.zhentao.domain.City;
import com.zhentao.domain.Province;
import com.zhentao.service.AreaService;
import com.zhentao.service.CityService;
import com.zhentao.service.ProvinceService;
import jakarta.annotation.PostConstruct;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

@SpringBootApplication
@MapperScan("com.zhentao.mapper")
@EnableConfigurationProperties(value = {MinioProperties.class})
public class StartApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);
    }
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private AreaService areaService;
    @Autowired
    private CityService cityService;
    @Autowired
    private ProvinceService provinceService;
    @PostConstruct
    void started() {
        if (StringUtils.checkValNotNull(stringRedisTemplate.opsForValue().get("chinese")))return;
        List<Province> list = provinceService.list();
        for (Province province : list) {
            if (StringUtils.checkValNotNull(province.getId())) {
                List<City> list1 = cityService.list(Wrappers.<City>lambdaQuery().eq(City::getPid, province.getId()));
                province.setCityList(list1);
                for (City city : list1) {
                    if(StringUtils.checkValNotNull(city.getId())){
                        List<Area> list2 = areaService.list(Wrappers.<Area>lambdaQuery().eq(Area::getPid, city.getId()));
                        city.setAreaList(list2);
                    }
                }
            }
        }
        stringRedisTemplate.opsForValue().set("chinese", JSON.toJSONString(list));
    }

    @Bean("tong-exchange")
    public Exchange exChangeBean(){
        return ExchangeBuilder.topicExchange("tong-exchange").durable(true).build();
    }
    @Bean("tong-queue")
    public Queue queue(){
        return QueueBuilder.durable("tong-queue").build();
    }
    @Bean
    public Binding ballsBillboard(@Qualifier("tong-queue") Queue queue, @Qualifier("tong-exchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
}